package com.lx.api;

import com.lx.api.request.DishPrintPlanObject;
import com.lx.api.request.TablePrintPlanObject;
import com.lx.api.response.*;
import com.lx.model.*;
import com.lx.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value ="api")
public class SettingController extends BaseRestController {

    @Autowired
    private SettingService settingService;

    @RequestMapping(value = "/systemparam/{shopId}",method = RequestMethod.GET)
    public Object getSystemParam(@PathVariable("shopId") String shopId)throws Exception{
        return settingService.getSystemParam(new Long(shopId));
    }

    @RequestMapping(value = "/systemparam",method = RequestMethod.POST)
    public Object saveSystemParam(@RequestBody PrintSystemParam paramVo)throws Exception{
        PrintSystemParam param = settingService.getSystemParam(paramVo.getShopId());
        if(param==null){
            param = new PrintSystemParam();
            param.setShopId(paramVo.getShopId());
        }
        param.setServiceStartupFlag(paramVo.getServiceStartupFlag());
        param.setSystemStartupFlag(paramVo.getSystemStartupFlag());
        param.setBackupPlanSec(paramVo.getBackupPlanSec());
        param.setPrintOrderMin(paramVo.getPrintOrderMin());
        settingService.saveSystemParam(param);
        return null;
    }

    @RequestMapping(value = "/kitchenFormat/{shopId}",method = RequestMethod.GET)
    public Object getKitchenFormat(@PathVariable("shopId") String shopId)throws Exception{
        return settingService.getPrintKitchenFormat(new Long(shopId));
    }

    @RequestMapping(value = "/kitchenFormat",method = RequestMethod.POST)
    public Object saveKitchenFormat(@RequestBody PrintKitchenFormat vo)throws Exception{
        PrintKitchenFormat format = settingService.getPrintKitchenFormat(vo.getShopId());
        if(format!=null){
            vo.setId(format.getId());
            vo.setCreateTime(format.getCreateTime());
        }
        settingService.savePrintKitchenFormat(vo);
        return null;
    }

    @RequestMapping(value = "/billFormat/{shopId}",method = RequestMethod.GET)
    public Object getBillFormat(@PathVariable("shopId") String shopId)throws Exception{
        return settingService.getPrintBillFormat(new Long(shopId));
    }

    @RequestMapping(value = "/billFormat",method = RequestMethod.POST)
    public Object saveBillFormat(@RequestBody PrintBillFormat vo)throws Exception{
        PrintBillFormat format = settingService.getPrintBillFormat(vo.getShopId());
        if(format!=null){
            vo.setId(format.getId());
            vo.setCreateTime(format.getCreateTime());
        }
        settingService.savePrintBillFormat(vo);
        return null;
    }

    @RequestMapping(value = "/printPlan/{shopId}",method = RequestMethod.GET)
    public Object getPrintPlan(@PathVariable("shopId") String shopId)throws Exception{
        List<PrintPlan> plans =  settingService.findPrintPlan(new Long(shopId));
        PrintPlanObject object = new PrintPlanObject();
        if (plans!=null&&plans.size()>0){
            object.setPlans(plans);
            object.setRecNum(plans.size());
        }
        return object;
    }

    @RequestMapping(value = "/printPlan/{planId}",method = RequestMethod.DELETE)
    public Object deletePrintPlan(@PathVariable("planId") String planId)throws Exception{
        settingService.deletePrintPlanById(new Long(planId));
        return null;
    }

    @RequestMapping(value = "/printPlan",method = RequestMethod.POST)
    public Object savePrintPlan(@RequestBody PrintPlan vo)throws Exception{
        Long id = settingService.savePrintPlan(vo);
        vo.setId(id);
        return vo;
    }

    @RequestMapping(value = "/dishType/{merchantsId}/{shopId}",method = RequestMethod.GET)
    public Object getDishType(@PathVariable("merchantsId") String merchantsId,@PathVariable("shopId") String shopId)throws Exception{
        List<TypeInfo> types =  settingService.findDishType(new Long(merchantsId),new Long(shopId));
        TypeObject object = new TypeObject();
        if (types!=null&&types.size()>0){
            object.setPrintDishTypeList(types);
            object.setRecNum(types.size());
        }
        return object;
    }

    @RequestMapping(value = "/shopDish/{merchantsId}/{shopId}",method = RequestMethod.GET)
    public Object queryShopDish(@PathVariable("merchantsId") String merchantsId,@PathVariable("shopId") String shopId,String dishTypeNo,String shopDishNo)throws Exception{
        List<RelateItemInfo> dishes =  settingService.findShopDishForRelate(new Long(merchantsId),new Long(shopId),dishTypeNo==null?null:new Long(dishTypeNo),shopDishNo);
        RelateItemObject object = new RelateItemObject();
        if (dishes!=null&&dishes.size()>0){
            object.setRelateItemInfoList(dishes);
            object.setRecNum(dishes.size());
        }
        return object;
    }

    @RequestMapping(value = "/shopDishPrintPlan",method = RequestMethod.POST)
    public Object saveShopDishPrintPlan(@RequestBody DishPrintPlanObject object)throws Exception{
        settingService.saveDishPrintPlans(object.getPrintDishPlans());
        return null;
    }

    @RequestMapping(value = "/tableType/{shopId}",method = RequestMethod.GET)
    public Object getTableType(@PathVariable("shopId") String shopId)throws Exception{
        List<TypeInfo> types =  settingService.findTableType(new Long(shopId));
        TypeObject object = new TypeObject();
        if (types!=null&&types.size()>0){
            object.setPrintDishTypeList(types);
            object.setRecNum(types.size());
        }
        return object;
    }

    @RequestMapping(value = "/shopTable/{shopId}",method = RequestMethod.GET)
    public Object queryShopTable(@PathVariable("shopId") String shopId,String tableTypeName,String tableNo)throws Exception{
        List<RelateItemInfo> dishes =  settingService.findShopTableForRelate(new Long(shopId),tableTypeName,tableNo);
        RelateItemObject object = new RelateItemObject();
        if (dishes!=null&&dishes.size()>0){
            object.setRelateItemInfoList(dishes);
            object.setRecNum(dishes.size());
        }
        return object;
    }

    @RequestMapping(value = "/shopTablePrintPlan",method = RequestMethod.POST)
    public Object saveShopTablePrintPlan(@RequestBody TablePrintPlanObject object)throws Exception{
        settingService.saveTablePrintPlans(object.getPrintTablePlans());
        return null;
    }





}
